﻿using IntroEntityFramework.Model;
using IntroEntityFramework.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace IntroEntityFramework.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            List<Appointment> model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Appointments.Where(x => x.IsDeleted == false).ToList();
            }
            return View(model);
        }

        public ActionResult Details(int id)
        {
            Appointment model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Appointments.FirstOrDefault(x => x.AppointmentID == id);
            }
            return View(model);
        }

        public ActionResult DeleteAppointment(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Appointment model = db.Appointments.FirstOrDefault(x => x.AppointmentID == id);
                if (model != null)
                {
                    model.IsDeleted = true;
                    db.SaveChanges();
                }
            }

            return RedirectToAction("Index");
        }

        public ActionResult EditAppointment(int id)
        {
            Appointment model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Appointments.FirstOrDefault(x => x.AppointmentID == id);
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult EditAppointment(Appointment model)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Appointment apt = db.Appointments.FirstOrDefault(x => x.AppointmentID == model.AppointmentID);
                if (apt != null)
                {
                    apt.Description = model.Description;
                    apt.TimeEnd = model.TimeEnd;
                    apt.TimeStart = model.TimeStart;
                    apt.UserWith = model.UserWith;
                    db.SaveChanges();
                }
            }
            return RedirectToAction("Details", new { id = model.AppointmentID });
        }

        public ActionResult AddAppointment()
        {
            return View(new Appointment());
        }

        [HttpPost]
        public ActionResult AddAppointment(Appointment model)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                db.Appointments.Add(model);
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}